import cv2
import numpy as np
img = cv2.imread(r"/\images\005.jpeg")
img0 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
height = img.shape[0]
width = img.shape[1]
dsize = (width, height)
'''
全局阈值化处理
'''
# 二值化阈值处理[黑白阈值，接收单通道一般]
ret1, img1 = cv2.threshold(img0, 150, 255, cv2.THRESH_BINARY)

# 反二值化阈值处理
ret2, img2 = cv2.threshold(img0, 150, 255, cv2.THRESH_BINARY_INV)

# 截断处理
ret3, img3 = cv2.threshold(img0, 150, 255, cv2.THRESH_TRUNC)

# 超阈值零处理
ret4, img4 = cv2.threshold(img0, 150, 255, cv2.THRESH_TOZERO_INV)

# 低阈值零处理
ret5, img5 = cv2.threshold(img0, 150, 255, cv2.THRESH_TOZERO)

# Otsu算法阈值处理
ret6, img6 = cv2.threshold(img0, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# 三角算法阈值化处理
ret7, img7 = cv2.threshold(img0, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_TRIANGLE)

'''
局部阈值处理：自适应阈值处理
'''

img8 = cv2.adaptiveThreshold(img0, 255,
                             cv2.ADAPTIVE_THRESH_MEAN_C,
                             cv2.THRESH_BINARY,
                             5, 10)

pic = [("img", img), ("img0", img0), ("img1", img1),
       ("img2", img2), ("img3", img3), ("img4", img4),
       ("img5", img5), ("img6", img6), ("img7", img7),
       ("img8", img8)]

for name, val in pic:
    cv2.imshow(name, val)
    key = cv2.waitKey(5000)
    if key == 27:
        break
cv2.destroyAllWindows()
